<template>
  <div class="app-container">
    <div class="module-title">
      <svg-icon icon-class="log" style="font-size:28px;margin-right:8px;"/>
      <span>收支记录</span>
      <span class="module-desc">管理每月收支明细</span>
    </div>
    <div class="module-card">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="月份" prop="month">
          <el-date-picker clearable
            v-model="queryParams.month"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择月份">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['record:record:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['record:record:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['record:record:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['record:record:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="记录ID" align="center" prop="recordId" />
        <el-table-column label="月份" align="center" prop="month" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.month, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="付款总计" align="center" prop="paymentTotal">
          <template slot-scope="scope">
            <span class="payment-amount">¥{{ scope.row.paymentTotal || '0.00' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="收款总计" align="center" prop="receiptTotal">
          <template slot-scope="scope">
            <span class="receipt-amount">¥{{ scope.row.receiptTotal || '0.00' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="其他费用总计" align="center" prop="expenseTotal">
          <template slot-scope="scope">
            <span class="expense-amount">¥{{ scope.row.expenseTotal || '0.00' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="其他收入总计" align="center" prop="incomeTotal">
          <template slot-scope="scope">
            <span class="income-amount">¥{{ scope.row.incomeTotal || '0.00' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="余额" align="center" prop="balance">
          <template slot-scope="scope">
            <span class="balance-amount">¥{{ scope.row.balance || '0.00' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['record:record:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['record:record:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    
    <!-- 添加或修改收支记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="月份" prop="month">
              <el-date-picker clearable
                v-model="form.month"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择月份">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="付款总计" prop="paymentTotal">
              <el-input v-model="form.paymentTotal" placeholder="请输入付款总计">
                <template slot="prepend">¥</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="收款总计" prop="receiptTotal">
              <el-input v-model="form.receiptTotal" placeholder="请输入收款总计">
                <template slot="prepend">¥</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="其他费用总计" prop="expenseTotal">
              <el-input v-model="form.expenseTotal" placeholder="请输入其他费用总计">
                <template slot="prepend">¥</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="其他收入总计" prop="incomeTotal">
              <el-input v-model="form.incomeTotal" placeholder="请输入其他收入总计">
                <template slot="prepend">¥</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="余额" prop="balance">
              <el-input v-model="form.balance" placeholder="自动计算" disabled>
                <template slot="prepend">¥</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRecord, getRecord, delRecord, addRecord, updateRecord } from "@/api/record/record"

export default {
  activated(){
      this.getList()
  },
  name: "Record",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 收支记录表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        month: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询收支记录列表 */
    getList() {
      this.loading = true
      listRecord(this.queryParams).then(response => {
        this.recordList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        recordId: null,
        month: null,
        paymentTotal: null,
        receiptTotal: null,
        expenseTotal: null,
        incomeTotal: null,
        balance: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.recordId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push('/record/record/add')
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const recordId = row.recordId || this.ids
      getRecord(recordId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改收支记录"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // 创建提交数据，排除自动计算的字段
          const submitData = {
            recordId: this.form.recordId,
            month: this.form.month,
            paymentTotal: this.form.paymentTotal,
            receiptTotal: this.form.receiptTotal,
            expenseTotal: this.form.expenseTotal,
            incomeTotal: this.form.incomeTotal,
            remark: this.form.remark
          }
          
          if (this.form.recordId != null) {
            updateRecord(submitData).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addRecord(submitData).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const recordIds = row.recordId || this.ids
      this.$modal.confirm('是否确认删除收支记录编号为"' + recordIds + '"的数据项？').then(function() {
        return delRecord(recordIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('record/record/export', {
        ...this.queryParams
      }, `record_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>

<style scoped lang="scss">
@import "~@/assets/styles/module-card.scss";

.module-title {
  display: flex;
  align-items: center;
  font-size: 22px;
  font-weight: 700;
  color: #2980b9;
  margin-bottom: 18px;
  .module-desc {
    font-size: 14px;
    color: #888;
    font-weight: 400;
    margin-left: 18px;
  }
}

.payment-amount {
  color: #e74c3c;
  font-weight: 600;
}

.receipt-amount {
  color: #27ae60;
  font-weight: 600;
}

.expense-amount {
  color: #f39c12;
  font-weight: 500;
}

.income-amount {
  color: #3498db;
  font-weight: 500;
}

.balance-amount {
  color: #2980b9;
  font-weight: 700;
  font-size: 14px;
}

.el-table {
  .el-table__body tr:hover > td {
    background: #eaf6ff !important;
  }
}

.el-dialog {
  .el-form-item {
    margin-bottom: 20px;
  }
  
  .el-input-group__prepend {
    background: #f0f4fa;
    color: #2980b9;
    font-weight: 600;
    border-color: #dbeafe;
  }
}
</style>
